<?php declare(strict_types=1);
/**
 * This file is part of Swoft.
 *
 * @link     https://swoft.org
 * @document https://swoft.org/docs
 * @contact  group@swoft.org
 * @license  https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace App\Http\Controller;

use App\Model\Entity\Template;
use App\Model\Entity\TemplateSendLog;
use App\Template\Log\Record;
use App\Template\Log\Sync;
use App\Template\Queue\Consumer;
use App\Template\Queue\Producer;
use App\Template\Type\Normal;
use Swoft;
use Swoft\Exception\SwoftException;
use Swoft\Http\Message\ContentType;
use Swoft\Http\Message\Response;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\View\Renderer;
use Throwable;
use function context;

/**
 * Class HomeController
 * @Controller()
 */
class HomeController
{
    /**
     * @RequestMapping("/")
     * @throws Throwable
     */
    public function index()
    {
        /** @var Renderer $renderer */
        /*$renderer = Swoft::getBean('view');
        $content  = $renderer->render('home/index');

        return context()->getResponse()->withContentType(ContentType::HTML)->withContent($content);*/



        Template::query()->select('id')->select('id')->chunk(100, function ($results, $page){
            foreach ($results as $item){
                $log = new Sync($item['id'] ?? 0);
                $log->execute();
            }
        });

        return 'a';
        //生产队列
        $producer = new Producer();
        $producer->scan('zhangshanabc');
        $producer->scan('zhangshan');
        $producer(function ($result = null){
            //do
        });

        //消费队列
        $consumer = new Consumer();
        $consumer->scan('zhangshanabc');
        $consumer->scan('zhangshan');
        $consumer(function ($result = null){
            //do
        });
    }

    /**
     * @RequestMapping("/t")
     *
     * @return Response
     * @throws SwoftException
     */
    public function timer()
    {
        Swoft\Timer::tick(100,function($timerId){
            $log = new Record(rand(1,2),rand(0,1));
            $log->execute();
            $log = null;
        });

        Swoft\Timer::tick(1000,function($timerId){
            Template::query()->select('id')->select('id')->chunk(100, function ($results, $page){
                foreach ($results as $item){
                    $log = new Sync($item['id'] ?? 0);
                    $log->execute();
                    $log = null;
                }
            });
        });

        /*\Swoft\Timer::tick(1, function ($timerId) {
            // To to something
            $producer = new \App\Template\Queue\Producer();
            $producer->scan('zhangshanabc');
            $producer->scan('zhangshan');
            $producer(function ($result = null){
                //do
            });
        });

        \Swoft\Timer::tick(1, function ($timerId) {
            // To to something
            //消费队列
            $consumer = new \App\Template\Queue\Consumer();
            $consumer->scan('zhangshanabc');
            $consumer->scan('zhangshan');
            $consumer(function ($result = null){
                //do
            });
        });*/
    }

    /**
     * @RequestMapping("/hi")
     *
     * @return Response
     * @throws SwoftException
     */
    public function hi(): Response
    {
        return context()->getResponse()->withContent('hi');
    }

    /**
     * @RequestMapping("/hello[/{name}]")
     * @param string $name
     *
     * @return Response
     * @throws SwoftException
     */
    public function hello(string $name): Response
    {
        return context()->getResponse()->withContent('Hello' . ($name === '' ? '' : ", {$name}"));
    }
}
